


function HexToBase64(hex) {
	var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var base64 = "";
	var group;
	for (var i = 0; i < 30; i += 6) {
		group = parseInt(hex.substr(i, 6), 16);
		base64 += base64Digits[(group >> 18) & 0x3f];
		base64 += base64Digits[(group >> 12) & 0x3f];
		base64 += base64Digits[(group >> 6) & 0x3f];
		base64 += base64Digits[group & 0x3f];
	}
	group = parseInt(hex.substr(30, 2), 16);
	base64 += base64Digits[(group >> 2) & 0x3f];
	base64 += base64Digits[(group << 4) & 0x3f];
	base64 += "==";
	return base64;
}

function NUUID(uuid) {
	var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
	var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
	var b = hex.substr(10, 2) + hex.substr(8, 2);
	var c = hex.substr(14, 2) + hex.substr(12, 2);
	var d = hex.substr(16, 16);
	hex = a + b + c + d;
	var base64 = HexToBase64(hex);
	return new BinData(3, base64);
}

function generateUUID() {
	var d = new Date().getTime();
	var uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
		var r = (d + Math.random() * 16) % 16 | 0;
		d = Math.floor(d / 16);
		return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
	});
	return NUUID(uuid);
};



use ClientPlatform

////Remove Criteria Type 10 from CriteriaLevel2, CriteriaLevel3, TemplateCriteria and OldTemplateCriteria
////remove items from CriteriaLevel2
//db.CriteriaLevel2.remove({Name: "Key ESG Issues"});

////remove items from CriteriaLevel3
//db.CriteriaLevel3.remove({Name: "Key ESG Issue 1"});
//db.CriteriaLevel3.remove({Name: "Key ESG Issue 2"});
//db.CriteriaLevel3.remove({Name: "Key ESG Issue 3"});

////remove items from TemplateCriteria
//db.TemplateCriteria.remove({CriteriaType: 10});

////remove items from OldTemplateCriteria
//db.OldTemplateCriteria.remove({CriteriaType: 10});

//get CriteriaLevel1 item by name ("ESG Performance")
var criteriaLevel1Item = db.CriteriaLevel1.findOne({Name: "ESG Performance"});
print(criteriaLevel1Item);
//add CriteriaLevel2 item
var criteriaLevel2Item =
{
	_id: generateUUID(),
	CriteriaLevel1Id : criteriaLevel1Item._id,
	Name: "Key ESG Issues",
	UniverseFeature : 13,
	ShortName : "KeyESGIssue",
	Order: 8
}
db.CriteriaLevel2.save(criteriaLevel2Item);

var numberCriteriaLevel3 = db.CriteriaLevel3.count();
//numberCriteriaLevel3++;
//add CriteriaLevel3 item
var criteriaLevel3Item1 = 
{
	_id : generateUUID(),
	Name : "Key ESG Issue 1",
	ShortName : "KeyESGIssue",
	Order : numberCriteriaLevel3 + 3,
	CriteriaLevel2Id : criteriaLevel2Item._id,
	Category : "None"
}
db.CriteriaLevel3.save(criteriaLevel3Item1);

//numberCriteriaLevel3++;
var criteriaLevel3Item2 = 
{
	_id : generateUUID(),
	Name : "Key ESG Issue 2",
	ShortName : "KeyESGIssue",
	Order : numberCriteriaLevel3 + 2,
	CriteriaLevel2Id : criteriaLevel2Item._id,
	Category : "None"
}
db.CriteriaLevel3.save(criteriaLevel3Item2);

//numberCriteriaLevel3++;
var criteriaLevel3Item3 = 
{
	_id : generateUUID(),
	Name : "Key ESG Issue 3",
	ShortName : "KeyESGIssue",
	Order : numberCriteriaLevel3 + 1,
	CriteriaLevel2Id : criteriaLevel2Item._id,
	Category : "None"
}
db.CriteriaLevel3.save(criteriaLevel3Item3);

//add TemplateCriteria items
//for criteriaLevel3Item1
var templateCriteria1Name = 
{
	_id : generateUUID(),
	Name : "Key ESG Issue 1-Name",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 1-Name",
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaLevel3Id : criteriaLevel3Item1._id,
	CriteriaType : 10,
	IsScreenable : false
}
var templateCriteria1Comment = 
{
	_id : generateUUID(),
	Name : "Key ESG Issue 1-Comment",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 1-Comment",
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaLevel3Id : criteriaLevel3Item1._id,
	CriteriaType : 10,
	IsScreenable : false
}

//for criteriaLevel3Item2
var templateCriteria2Name = 
{
	_id : generateUUID(),
	Name : "Key ESG Issue 2-Name",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 2-Name",
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaLevel3Id : criteriaLevel3Item2._id,
	CriteriaType : 10,
	IsScreenable : false
}
var templateCriteria2Comment = 
{
	_id : generateUUID(),
	Name : "Key ESG Issue 2-Comment",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 2-Comment",
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaLevel3Id : criteriaLevel3Item2._id,
	CriteriaType : 10,
	IsScreenable : false
}

//for criteriaLevel3Item3
var templateCriteria3Name = 
{
	_id : generateUUID(),
	Name : "Key ESG Issue 3-Name",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 3-Name",
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaLevel3Id : criteriaLevel3Item3._id,
	CriteriaType : 10,
	IsScreenable : false
}
var templateCriteria3Comment = 
{
	_id : generateUUID(),
	Name : "Key ESG Issue 3-Comment",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 3-Comment",
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaLevel3Id : criteriaLevel3Item3._id,
	CriteriaType : 10,
	IsScreenable : false
}
db.TemplateCriteria.save(templateCriteria1Name);
db.TemplateCriteria.save(templateCriteria1Comment);
db.TemplateCriteria.save(templateCriteria2Name);
db.TemplateCriteria.save(templateCriteria2Comment);
db.TemplateCriteria.save(templateCriteria3Name);
db.TemplateCriteria.save(templateCriteria3Comment);

var numberOfOldTemplateCriteria = db.OldTemplateCriteria.count();
numberOfOldTemplateCriteria++;
//add to OldTemplateCriteria
var oldTemplateCriteria1Name = 
{
	_id : templateCriteria1Name._id,
	Name : "Key ESG Issue 1-Name",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 1-Name",
	Score : null,
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaType : 10,
	FilterId : null,
	CompaniesIdsResult : null,
	Order : numberOfOldTemplateCriteria
}

numberOfOldTemplateCriteria++;
var oldTemplateCriteria1Comment = 
{
	_id : templateCriteria1Comment._id,
	Name : "Key ESG Issue 1-Comment",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 1-Comment",
	Score : null,
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaType : 10,
	FilterId : null,
	CompaniesIdsResult : null,
	Order : numberOfOldTemplateCriteria
}

numberOfOldTemplateCriteria++;
var oldTemplateCriteria2Name = 
{
	_id : templateCriteria2Name._id,
	Name : "Key ESG Issue 2-Name",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 2-Name",
	Score : null,
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaType : 10,
	FilterId : null,
	CompaniesIdsResult : null,
	Order : numberOfOldTemplateCriteria
}

numberOfOldTemplateCriteria++;
var oldTemplateCriteria2Comment = 
{
	_id : templateCriteria2Comment._id,
	Name : "Key ESG Issue 2-Comment",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 2-Comment",
	Score : null,
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaType : 10,
	FilterId : null,
	CompaniesIdsResult : null,
	Order : numberOfOldTemplateCriteria
}

numberOfOldTemplateCriteria++;
var oldTemplateCriteria3Name = 
{
	_id : templateCriteria3Name._id,
	Name : "Key ESG Issue 3-Name",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 3-Name",
	Score : null,
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaType : 10,
	FilterId : null,
	CompaniesIdsResult : null,
	Order : numberOfOldTemplateCriteria
}

numberOfOldTemplateCriteria++;
var oldTemplateCriteria3Comment = 
{
	_id : templateCriteria3Comment._id,
	Name : "Key ESG Issue 3-Comment",
	CriteriaEntity : "ScreeningData",
	CriteriaEntityField : "Key ESG Issue 3-Comment",
	Score : null,
	CriteriaLevel2Id : criteriaLevel2Item._id,
	CriteriaType : 10,
	FilterId : null,
	CompaniesIdsResult : null,
	Order : numberOfOldTemplateCriteria
}
db.OldTemplateCriteria.save(oldTemplateCriteria1Name);
db.OldTemplateCriteria.save(oldTemplateCriteria1Comment);
db.OldTemplateCriteria.save(oldTemplateCriteria2Name);
db.OldTemplateCriteria.save(oldTemplateCriteria2Comment);
db.OldTemplateCriteria.save(oldTemplateCriteria3Name);
db.OldTemplateCriteria.save(oldTemplateCriteria3Comment);





